package parallels.servertech.server.nio;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.util.logging.Logger;

public class NonBlockingChannelOutputStream extends OutputStream {
	private static final Logger log = Logger.getLogger("output");
	
	// TODO move to superclass
	private static final int BUF_SIZE = 4096;
	
	private final ByteChannel channel;
	private final ByteBuffer buffer;
	
	public NonBlockingChannelOutputStream(ByteChannel channel) {
		this.channel = channel;
		this.buffer = ByteBuffer.allocate(BUF_SIZE);
	}

	@Override
	public void write(int oneByte) throws IOException {
		buffer.clear();
		buffer.put((byte)oneByte);
		buffer.flip();
		channel.write(buffer);
	}
}
